package gameserver.dataholders;

import gameserver.model.templates.item.ItemTemplate;
import gnu.trove.TIntObjectHashMap;

import java.util.List;

import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.*;

import org.apache.log4j.Logger;

@XmlRootElement(name = "item_templates")
@XmlAccessorType(XmlAccessType.FIELD)
public class ItemData
{
	private static Logger log = Logger.getLogger(ItemData.class);
	
	@XmlElement(name = "item_template")
	private List<ItemTemplate> its;
	
	private TIntObjectHashMap<ItemTemplate> items;
	
	void afterUnmarshal(Unmarshaller u, Object parent)
	{
		items = new TIntObjectHashMap<ItemTemplate>();
		for(ItemTemplate it : its)
		{
			items.put(it.getTemplateId(), it);
		}
		its = null;
	}
	
	public int size() {
        return items.size();
    }
	
	public ItemTemplate getItemTemplate(int itemId) {
        ItemTemplate itemTemplate = items.get(itemId);
        if (itemTemplate == null)
            log.error("Item was not populated correctly. Item template is missing for item id: " + itemId);
        return itemTemplate;
    }
}
